﻿////////////////////////////////////////////////////////////////////////////////////////////
// File: PDrag.cs                                                                         //
// Author: Mike Binnix                                                                    //
// Desc: force generator to simulate drag.  This is normally unneeded in simple           //
//       simulations.  Can be overwritten for times when more complex drag effects are    //
//       needed.  Such as when creating a driving or flying simulation.                   //
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using Microsoft.Xna.Framework;

namespace SlothEngine.Physics.Forces
{
    class PDrag : IPForceGenerator
    {
        #region Attributes
        protected float dragCoeff;
        protected float dragCoeffSquare;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the drag coeffificient.
        /// </summary>
        public float DragCoeffificent
        {
            get
            {
                return dragCoeff;
            }
            set
            {
                dragCoeff = value;
            }
        }
        /// <summary>
        /// Gets or sets the square drag coeffificient.
        /// </summary>
        public float SquareDragCoeffificent
        {
            get
            {
                return dragCoeffSquare;
            }
            set
            {
                dragCoeffSquare = value;
            }
        }
        #endregion

        #region Constructors
        public PDrag(float drag1)
        {
            dragCoeff = drag1;
            dragCoeffSquare = 0.0f;
        }
        public PDrag(float drag1, float drag2)
        {
            dragCoeff = drag1;
            dragCoeffSquare = drag2;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Applies the drag force to object.
        /// </summary>
        /// <param name="gameTime">Time since last update.</param>
        /// <param name="particle">particle to change.</param>
        public virtual void Update(GameTime gameTime, XNAParticle particle)
        {
            Vector2 force = particle.Velocity;

            // Find total drag
            float drag = (float)Math.Sqrt(force.X * force.X + force.Y * force.Y);
            drag = dragCoeff * drag + dragCoeffSquare * drag * drag;

            // Find final force to apply based off drag
            force.Normalize();
            force *= -drag;
            
            // Apply force
            particle.AddForce(force);
        }
        #endregion
    }
}
